import {Component, OnInit} from '@angular/core';
import {CommonService} from '../../common.service';
import {CookieService} from 'ngx-cookie';

@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
    longinInfo = {
        username: '',
        password: '',
        verify: '',
        key: '',
    };
    verify;
    key;

    constructor(private Service: CommonService, private Cookie: CookieService) {
    }

    ngOnInit() {
        this.getCode();

        console.log(this.Service.getItem('ref'))
    }

    /* 获取验证码 */
    getCode() {
        const $this = this;
        this.Service.post('login/verify', {}).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                $this.verify = data.img;
                $this.key = data.key;
                $this.longinInfo.key = data.key;
            });
        });
    }

    login() {
        const $this = this;
        let index = layer.load();
        this.Service.post('login/login', this.longinInfo).subscribe(res => {
            this.Service.resultHandler(res, function (msg, data) {
                layer.close(index);
                if (res['status'] == 1) {
                    layer.msg('登录成功');
                    $this.Service.setItem('user', JSON.stringify(data));
                    setTimeout(function () {
                        location.href = $this.Service.getItem('ref');
                    }, 3000);
                } else {
                    // 提示
                    layer.msg(msg);
                }
                $this.getCode();
            });
        });
    }
}
